package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.DeveloperDao;
import com.zwxbc.funplay.entity.Developer;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeveloperDaoImpl implements DeveloperDao {
	private static final String INSERT_DEVELOPER = "INSERT INTO developer (developer_name, developer_address, developer_username, developer_password, developer_contact, developer_email) VALUES (?, ?, ?, ?, ?, ?)";
	private static final String SELECT_DEVELOPER_BY_ID = "SELECT * FROM developer WHERE developer_id = ?";
	private static final String SELECT_ALL_DEVELOPERS = "SELECT * FROM developer";
	private static final String UPDATE_DEVELOPER = "UPDATE developer SET developer_name = ?, developer_address = ?, developer_username = ?, developer_password = ?, developer_contact = ?, developer_email = ? WHERE developer_id = ?";
	private static final String DELETE_DEVELOPER = "DELETE FROM developer WHERE developer_id = ?";
	private static final String LOGIN_DEVELOPER = "SELECT * FROM developer WHERE (developer_username = ? or developer_contact = ? or developer_email = ?) and developer_password=?";
	private static final String DEVELOPER_EXISTS = "SELECT COUNT(*) FROM developer WHERE developer_username = ? OR developer_contact = ? OR developer_email = ?";

	@Override
	public boolean isDeveloperExists(String username, String contact, String email) {
		int count = 0;
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DEVELOPER_EXISTS)) {
			preparedStatement.setString(1, username);
			preparedStatement.setString(2, contact);
			preparedStatement.setString(3, email);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					count = resultSet.getInt(1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count > 0;
	}

	@Override
	public int addDeveloper(Developer developer) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_DEVELOPER)) {
			preparedStatement.setString(1, developer.getDeveloperName());
			preparedStatement.setString(2, developer.getDeveloperAddress());
			preparedStatement.setString(3, developer.getDeveloperUsername());
			preparedStatement.setString(4, developer.getDeveloperPassword());
			preparedStatement.setString(5, developer.getDeveloperContact());
			preparedStatement.setString(6, developer.getDeveloperEmail());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public Developer developerLogin(Developer developer) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(LOGIN_DEVELOPER)) {
			preparedStatement.setString(1, developer.getDeveloperUsername());
			preparedStatement.setString(2, developer.getDeveloperContact());
			preparedStatement.setString(3, developer.getDeveloperEmail());
			preparedStatement.setString(4, developer.getDeveloperPassword());
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createDeveloperFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public Developer getDeveloperById(Long developerId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_DEVELOPER_BY_ID)) {
			preparedStatement.setLong(1, developerId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createDeveloperFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<Developer> getAllDevelopers() {
		List<Developer> developers = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_DEVELOPERS);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				developers.add(createDeveloperFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return developers;
	}

	@Override
	public int updateDeveloper(Developer developer) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_DEVELOPER)) {
			preparedStatement.setString(1, developer.getDeveloperName());
			preparedStatement.setString(2, developer.getDeveloperAddress());
			preparedStatement.setString(3, developer.getDeveloperUsername());
			preparedStatement.setString(4, developer.getDeveloperPassword());
			preparedStatement.setString(5, developer.getDeveloperContact());
			preparedStatement.setString(6, developer.getDeveloperEmail());
			preparedStatement.setLong(7, developer.getDeveloperId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteDeveloper(Long developerId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_DEVELOPER)) {
			preparedStatement.setLong(1, developerId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	// Helper 方法，从 ResultSet 中创建 Developer 对象
	private Developer createDeveloperFromResultSet(ResultSet resultSet) throws SQLException {
		Developer developer = new Developer();
		developer.setDeveloperId(resultSet.getLong("developer_id"));
		developer.setDeveloperName(resultSet.getString("developer_name"));
		developer.setDeveloperAddress(resultSet.getString("developer_address"));
		developer.setDeveloperUsername(resultSet.getString("developer_username"));
		developer.setDeveloperPassword(resultSet.getString("developer_password"));
		developer.setDeveloperContact(resultSet.getString("developer_contact"));
		developer.setDeveloperEmail(resultSet.getString("developer_email"));
		return developer;
	}

	public static void main(String[] args) {
		DeveloperDao developerDao = new DeveloperDaoImpl();
		List<Developer> allDevelopers = developerDao.getAllDevelopers();
		System.out.println(allDevelopers);
	}
}
